package com.atguigu.order.service.impl;

import com.atguigu.order.bean.Order;
import com.atguigu.order.feign.ProductFeignClient;
import com.atguigu.order.service.OrderService;
import com.atguigu.product.bean.Product;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * @author: dh
 * @date: 2025-03-22 19:52
 */
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private ProductFeignClient productFeignClient;

    @Override
    public Order createOrder(Long userId, Long productId) {

        Product product = productFeignClient.getProductById(productId);
        Order order = new Order();
        order.setId(1L);
        // 总金额
        order.setTotalAmount(product.getPrice().multiply(new BigDecimal(product.getNum())));
        order.setUserId(userId);
        order.setNickName("zhangsan");
        order.setAddress("shangguigu");
        // 远程查询商品列表
        order.setProductList(Arrays.asList(product));
        return order;
    }

    /**
     * 远程获取商品信息
     * @param productId
     * @return
     */
    private Product getProductFromRemote(Long productId) {

        // 1、获取到商品服务所在的所有机器IP + 端口号；
        List<ServiceInstance> instances = discoveryClient.getInstances("server-product");
        ServiceInstance instance = instances.get(0);
        // 2、获取商品服务的远程地址
        String url = "http://" + instance.getHost() + ":" + instance.getPort() + "/product/" + productId;
        log.info("远程请求：{}", url);
        // 3、给远程发送请求
        Product product = restTemplate.getForObject(url, Product.class);
        return product;
    }

    /**
     * 负载均衡实现远程调用
     * @param productId
     * @return
     */
    private Product getProductFromRemoteWithLoadBalancer(Long productId) {

        // 1、获取到商品服务所在的所有机器IP + 端口号；
        ServiceInstance instance = loadBalancerClient.choose("server-product");
        // 2、获取商品服务的远程地址
        String url = "http://" + instance.getHost() + ":" + instance.getPort() + "/product/" + productId;
        log.info("远程请求：{}", url);
        // 3、给远程发送请求
        Product product = restTemplate.getForObject(url, Product.class);
        return product;
    }

    /**
     * 注解式负载均衡实现远程调用
     * @param productId
     * @return
     */
    private Product getProductFromRemoteWithLoadBalancerAnnotation(Long productId) {

        String url = "http://server-product/product/" + productId;
        // 3、给远程发送请求
        Product product = restTemplate.getForObject(url, Product.class);
        return product;
    }
}
